{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bereitstellung der Methoden\n",
    "\n",
    "__ACHTUNG__: Der Mechanismus mit den Buttons funktioniert nur mit __JUPYTER NOTEBOOK!!!!!__ Nicht JUPYTER LAB!! Bitte mit ``jupyter notebook`` starten"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "23253be94c624140bf096cab0c3ae4c1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(Output(), Button(description='Run', style=ButtonStyle())))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "after clicking 'Run' the sim will run in background results available via thread.stats\n"
     ]
    }
   ],
   "source": [
    "from BPTK_Py.bptk import bptk\n",
    "import ipywidgets as widgets\n",
    "from threading import Thread\n",
    "out = widgets.Output()\n",
    "\n",
    "bptk = bptk()\n",
    "scenario_manager = bptk.scenario_manager_factory.scenario_managers[\"Covid19\"]\n",
    "model = scenario_manager.scenarios[\"base\"]\n",
    "\n",
    "stats = {}\n",
    "\n",
    "rounds = 1000\n",
    "stopped = True\n",
    "\n",
    "class threaded():\n",
    "    def __init__(self, model, rounds):\n",
    "        self.stopped = False\n",
    "        self.stats = {}\n",
    "        self.thread = Thread(target=self.threaded_sim,args=(model, rounds))\n",
    "        \n",
    "    def threaded_sim(self,model,rounds):\n",
    "        for i in range(0,rounds):\n",
    "            model.scheduler.run_step(model=model,sim_round=i,step=0)\n",
    "            self.stats = model.statistics()\n",
    "            if self.stopped:\n",
    "                break\n",
    "\n",
    "# Der thread\n",
    "thread = threaded(model, rounds)\n",
    "\n",
    "# der Button\n",
    "button = widgets.Button(description=\"Run\")\n",
    "\n",
    "def stop_threaded_sim(b):\n",
    "    stopped = True\n",
    "    button.on_click(start_threaded_sim)\n",
    "\n",
    "def start_threaded_sim(b):\n",
    "    with out:\n",
    "        try: # Try to start Thread\n",
    "            thread.stopped = False\n",
    "            thread.thread.start()\n",
    "            b.description = \"Stop\"\n",
    "        except RuntimeError: # Probably thread is running so we want to stop\n",
    "            thread.stopped =True\n",
    "            b.description= \"Run\"\n",
    "\n",
    "button.on_click(start_threaded_sim)\n",
    "\n",
    "display(widgets.HBox([out,button]))\n",
    "print(\"after clicking 'Run' the sim will run in background results available via thread.stats\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: {'person': {'HEALTHY': {'count': 6435,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 1247,\n",
       "    'infected': {'total': 186,\n",
       "     'max': 3,\n",
       "     'min': 0,\n",
       "     'mean': 0.1491579791499599}},\n",
       "   'INFECTED_HARD': {'count': 318,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 1: {'person': {'HEALTHY': {'count': 6287,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 1395,\n",
       "    'infected': {'total': 192,\n",
       "     'max': 2,\n",
       "     'min': 0,\n",
       "     'mean': 0.13763440860215054}},\n",
       "   'INFECTED_HARD': {'count': 318,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 2: {'person': {'HEALTHY': {'count': 6143,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 1539,\n",
       "    'infected': {'total': 206,\n",
       "     'max': 2,\n",
       "     'min': 0,\n",
       "     'mean': 0.13385315139701104}},\n",
       "   'INFECTED_HARD': {'count': 318,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 3: {'person': {'HEALTHY': {'count': 5989,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 1693,\n",
       "    'infected': {'total': 224, 'max': 3, 'min': 0, 'mean': 0.132309509746013}},\n",
       "   'INFECTED_HARD': {'count': 318,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 4: {'person': {'HEALTHY': {'count': 5833,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 1848,\n",
       "    'infected': {'total': 282,\n",
       "     'max': 3,\n",
       "     'min': 0,\n",
       "     'mean': 0.1525974025974026}},\n",
       "   'INFECTED_HARD': {'count': 319,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 5: {'person': {'HEALTHY': {'count': 5632,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 2049,\n",
       "    'infected': {'total': 289,\n",
       "     'max': 3,\n",
       "     'min': 0,\n",
       "     'mean': 0.14104441190824793}},\n",
       "   'INFECTED_HARD': {'count': 319,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 6: {'person': {'HEALTHY': {'count': 5429,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 2251,\n",
       "    'infected': {'total': 336,\n",
       "     'max': 2,\n",
       "     'min': 0,\n",
       "     'mean': 0.14926699244780098}},\n",
       "   'INFECTED_HARD': {'count': 320,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 7: {'person': {'HEALTHY': {'count': 5213,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 2467,\n",
       "    'infected': {'total': 377,\n",
       "     'max': 3,\n",
       "     'min': 0,\n",
       "     'mean': 0.15281718686663964}},\n",
       "   'INFECTED_HARD': {'count': 320,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 8: {'person': {'HEALTHY': {'count': 4959,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 2720,\n",
       "    'infected': {'total': 408, 'max': 3, 'min': 0, 'mean': 0.15}},\n",
       "   'INFECTED_HARD': {'count': 321,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 9: {'person': {'HEALTHY': {'count': 4709,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 2970,\n",
       "    'infected': {'total': 454,\n",
       "     'max': 2,\n",
       "     'min': 0,\n",
       "     'mean': 0.15286195286195287}},\n",
       "   'INFECTED_HARD': {'count': 321,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 10: {'person': {'HEALTHY': {'count': 4459,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 3220,\n",
       "    'infected': {'total': 458,\n",
       "     'max': 3,\n",
       "     'min': 0,\n",
       "     'mean': 0.1422360248447205}},\n",
       "   'INFECTED_HARD': {'count': 321,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 11: {'person': {'HEALTHY': {'count': 4201,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 3477,\n",
       "    'infected': {'total': 495,\n",
       "     'max': 3,\n",
       "     'min': 0,\n",
       "     'mean': 0.14236410698878343}},\n",
       "   'INFECTED_HARD': {'count': 322,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 12: {'person': {'HEALTHY': {'count': 3949,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 3728,\n",
       "    'infected': {'total': 501,\n",
       "     'max': 2,\n",
       "     'min': 0,\n",
       "     'mean': 0.1343884120171674}},\n",
       "   'INFECTED_HARD': {'count': 323,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 13: {'person': {'HEALTHY': {'count': 3718,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 3959,\n",
       "    'infected': {'total': 524,\n",
       "     'max': 3,\n",
       "     'min': 0,\n",
       "     'mean': 0.1323566557211417}},\n",
       "   'INFECTED_HARD': {'count': 323,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 14: {'person': {'HEALTHY': {'count': 3473,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 4204,\n",
       "    'infected': {'total': 590,\n",
       "     'max': 3,\n",
       "     'min': 0,\n",
       "     'mean': 0.14034253092293053}},\n",
       "   'INFECTED_HARD': {'count': 323,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 15: {'person': {'HEALTHY': {'count': 3213,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_LIGHT': {'count': 4463,\n",
       "    'infected': {'total': 622,\n",
       "     'max': 2,\n",
       "     'min': 0,\n",
       "     'mean': 0.13936813802375084}},\n",
       "   'INFECTED_HARD': {'count': 324,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 16: {'person': {'INFECTED_LIGHT': {'count': 4699,\n",
       "    'infected': {'total': 641,\n",
       "     'max': 3,\n",
       "     'min': 0,\n",
       "     'mean': 0.13641200255373484}},\n",
       "   'HEALTHY': {'count': 2977,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_HARD': {'count': 324,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 17: {'person': {'INFECTED_LIGHT': {'count': 4928,\n",
       "    'infected': {'total': 737,\n",
       "     'max': 3,\n",
       "     'min': 0,\n",
       "     'mean': 0.14955357142857142}},\n",
       "   'HEALTHY': {'count': 2748,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_HARD': {'count': 324,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}},\n",
       " 18: {'person': {'INFECTED_LIGHT': {'count': 5179,\n",
       "    'infected': {'total': 724,\n",
       "     'max': 3,\n",
       "     'min': 0,\n",
       "     'mean': 0.13979532728325933}},\n",
       "   'HEALTHY': {'count': 2496,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}},\n",
       "   'INFECTED_HARD': {'count': 325,\n",
       "    'infected': {'total': 0, 'max': 0, 'min': 0, 'mean': 0.0}}}}}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "thread.stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
